<?php

namespace app\controller\wmapi\partner;

use app\model\WaMembersAddress;
use Illuminate\Support\Facades;
use Illuminate\Support\Facades\Validator;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Admin;
use support\Request;
use app\model\WaAdmins;
use app\model\WaRoles;
use app\model\WaMembers;
use app\model\WaRules;
use app\model\WaDepartment;
use app\model\WaAdminRoles;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Cache;
use Tinywan\Validate\Validate;
use app\controller\wmapi\IndexController;

class MemberAddressController extends Validate
{
    /**
     * 客户地址管理 -列表-分页
     * @param Request $request
     * @return mixed
     */
    public function memberAddressList(Request $request)
    {
        $param = $request->all();
        $pageNum = page($param['pageNum']);
        $fields = ['*'];
        $condition = [];

        $page = page($param['page']);
        $rule = WaMembersAddress::list($page, $pageNum, $fields, $condition);

        return IndexController::successed($rule, '客户地址列表');
    }

    /**
     * 客户地址管理 -列表-不分页
     * @param Request $request
     * @return mixed
     */
    public function memberAddressAll(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $fields = ['*'];
        $condition = [];
        if ($param['country']) {
            $condition [] = ['country', 'like', '%' . $param['country'] . '%']; //
        }
        $rule = WaMembersAddress::all($fields, $condition);
        return IndexController::successed($rule, '客户地址列表');
    }

    /**
     * 客户地区管理 -列表-不分页
     * @param Request $request
     * @return mixed
     */
    public function memberAreaAll(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $fields = ['area'];
        $condition = [];
        if ($param['country']) {
            $condition [] = ['country', '=',$param['country']]; //
        }
        if ($param['area']) {
            $condition [] = ['area', 'like', '%' . $param['area'] . '%']; //
        }
        $rule = WaMembersAddress::all($fields, $condition);
        return IndexController::successed($rule, '客户地区列表');
    }


    /**
     * 客户-创建
     * @param Request $request
     * @return \support\Response
     */
    public function memberAddressCreate(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        /**
         * 验证规则
         */

        $data = [
            'country' => $param['country'],
            'area' => $param['area'],
            'created_at' => dateTime(),
        ];
       $members_address =  WaMembersAddress::find(['country' => $param['country'],
            'area' => $param['area']]);
        $members_address = json_decode($members_address);
        if ($members_address){
            return IndexController::errored('','该地址已存在');
        }

            \support\Db::beginTransaction();
            try {
                $res = WaMembersAddress::created($data);
                \support\Db::commit();
                return IndexController::successed($res, '客户地址创建成功');
            } catch (\Throwable $exception) {
                IndexController::logError('memberAddressCreate', $param, $adminId, $exception);
                \support\Db::rollBack();
                return IndexController::errored($exception, '客户地址创建失败');
            }


    }

    /**
     * 客户-更新
     * @param Request $request
     * @return \support\Response
     */
    public function memberAddressEdit(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        /**
         * 验证规则
         */
        $rule = [
            'name' => 'require|max:50',
            'maintainer_admin_id' => 'require',
            'phone' => 'require',
            'type' => 'require',
            'member_id' => 'require',
        ];

        $data = [
            'name' => $param['name'], // 客户名字/企业名称
            'country' => $param['country'],
//            'province' => $param['province'],
//            'city' => $param['city'],
            'area' => $param['area'],
            'area_code' => $param['area_code'],
            'address' => $param['address'],
            'phone' => $param['phone'],
            'email' => $param['email'],
            'passport_number' => $param['passport_number'],// 护照号码
            'maintainer_admin_id' => $param['maintainer_admin_id'],// 维护人(admin_id)
            'business_license_number' => $param['business_license_number'],// 营业执照号
            'tax_registration_number' => $param['tax_registration_number'],// 税务登记号
            'type' => $param['type'],// 1个人 2 企业
            'linkman' => $param['linkman'],
            'remark' => $param['remark'],// 备注
            'updated_at' => dateTime(),
        ];
        if ($param['status'] || $param['status'] == 0) {
            $data['status'] = $param['status']; // 0默认  1正常 2 禁用 4 删除
        }
        if ($param['channel_id']){ $data['channel_id'] = $param['channel_id']; }
        if ($param['term_id']){ $data['term_id'] = $param['term_id']; }

        $validate = \Tinywan\Validate\Facade\Validate::rule($rule);
        if (!$validate->check($param)) {
            return IndexController::errored($validate->getError());
        } else {
            $where = ['id' => $param['member_id']];
            $find = WaMembers::find($where);
            $find = json_decode($find);
            if ($find){
                if (property_exists($find, 'id')) {
                    if ($param['phone'] != $find->phone) {
                        return IndexController::errored('', '客户更新失败,手机号名已存在');
                    }
                }
            }

            $condition = [];
            $condition [] = ['id', '=', intval($param['member_id'])];
            \support\Db::beginTransaction();
            try {
                $res = WaMembers::updated($condition, $data);
                \support\Db::commit();
                return IndexController::successed($res, '客户更新成功');
            } catch (\Throwable $exception) {
                IndexController::logError('memberAddressEdit', $param, $adminId, $exception);
                \support\Db::rollBack();
                return IndexController::errored($exception, '客户更新失败');
            }
        }
    }

    /**
     * 客户-删除
     * @param Request $request
     * @return \support\Response
     */
    public function memberAddressDel(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $condition = [];
        $condition []= ['id', '=', $param['member_id']];

        $data = [
            'status' => 4,
            'updated_at' => dateTime(),
        ];
        \support\Db::beginTransaction();
        try {
            $res = WaMembers::updated($condition,$data);
            \support\Db::commit();
            return IndexController::successed($res, '客户-删除成功');
        } catch (\Throwable $exception) {
            IndexController::logError('memberAddressDel', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '客户-删除失败');
        }
    }

    /**
     * 管理员-详情
     * @param Request $request
     * @return \support\Response
     */
    public function memberAddressDetail(Request $request)
    {
//        process($request);
        $param = $request->all();
        $condition = [];
        $condition [] = ['id', '=', $param['member_id']];
        $rule = WaMembers::find($condition);
//        $rule = json_decode($rule);
//        $rule->role_id = json_decode($rule->role_id);
//        $rule = json_encode($rule);
        return IndexController::successed($rule, '客户信息');
    }


}